// components/videoUpload/videoUpload.js
const app = getApp();
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Component({
  /**
   * 组件的属性列表
   */
  properties: { 
    //上传地址   
    uploadFileUrl: {
      type: String,
      value: 'https://sys.bangnikeji.net/oss/headImgUpload?filepath=bangni/images/'
    },
   
  },

  options: {    
    //虚拟的，这样可以不作用样式
    virtualHost: true    
  },

  /**
   * 组件的初始数据
   */
  data: {
    //视频地址
    videoUrl:'',
    //显示视频播放器
    showVideo:false   
  },  
  
  pageLifetimes: {
    show: function() {

    }
  },

  /**
   * 组件的方法列表
   */
  methods: { 
    //选择视频并上传
    onChooseVideo(){
      let _this = this;
      wx.chooseVideo({
        sourceType: ['album','camera'],
        maxDuration: 60,
        camera: 'back',
        success(res) { 
          Toast.loading({
            duration: 0,
            forbidClick: true,          
            message: '视频上传中...'
          });         
          wx.uploadFile({
            url: _this.properties.uploadFileUrl,
            filePath: res.tempFilePath,
            name: 'file',
            success(res1) {
              Toast.clear();           
              let url = JSON.parse(res1.data).data; 
              _this.setData({
                showVideo:true,
                videoUrl: url               
              })
            }, 
            fail(){
              Toast('上传失败请重试');               
            }              
          });
        }
      })
    },
        

    //把图片路径传给父页面
    getUploadUrl(){
      this.triggerEvent('getUploadUrl', this.data.imageList);
    }

  }
})
